﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
//using System.Xml.Linq;
using Telerik.Web.UI;
using KPIS.Utils;
using KPIS.DBM;
using KPIS.GERP.GHRMS.IBLL;
using KPIS.GERP.GHRMS.BLL;
using KPIS.GERP.GHRMS.MODEL;

namespace KPIS.GERP.GHRMS.APP.App_UI.UserControls
{
    public partial class PR_PromoteEmployee : System.Web.UI.UserControl
    {
        private SsMng ssMng = new SsMng();
        private IEmployeeBLL empBLL = new EmployeeBLL();
        private IJobSiteBLL jsBLL = new JobSiteBLL();
        private PromoteEmployeeBLL prBLL = new PromoteEmployeeBLL();

        private EmployeeInfo infoEmp;
        private object _dataItem = null;
        protected void Page_Load(object sender, EventArgs e)
        {
            if (DataItem != null)
            {
                ListJobSite();

                if (!(DataItem is Telerik.Web.UI.GridInsertionObject))
                {
                    lblEmpSeq.Text = DataBinder.Eval(DataItem, "emp_seq").ToString();
                    txtEmp.Text = DataBinder.Eval(DataItem, "emp_text").ToString();
                    lblPositionName.Text = DataBinder.Eval(DataItem, "pos_name").ToString();
                    lblClassName.Text = DataBinder.Eval(DataItem, "class_name").ToString();
                    cmbAprJobSite.SelectedValue = DataBinder.Eval(DataItem, "apr_job_site_seq").ToString();
                    ListSubScore();
                    txtScore_TextChanged(sender, e);
                    cmbPrmJobSite.SelectedValue = DataBinder.Eval(DataItem, "prm_job_site_seq").ToString();
                    rdoPromote.SelectedValue = DataBinder.Eval(DataItem, "promote").ToString();
                }
                else
                {
                    cmbAprJobSite.SelectedValue = Session["JSID"].ToString();
                    rdoPromote.SelectedValue = "0";
                }
                cmbAprJobSite.Enabled = !(DataItem is Telerik.Web.UI.GridInsertionObject);
            }
        }
        public object DataItem
        {
            get {return this._dataItem;}
            set {this._dataItem = value;}
        }
        protected void ListJobSite()
        {
            JobSiteTypeInfo Info = new JobSiteTypeInfo();
            IList<JobSiteInfo> InfoList = jsBLL.ListJobSiteByTypes(SystemInfo._JobsiteInternal);

            cmbAprJobSite.DataSource = InfoList;
            cmbAprJobSite.DataBind();

            cmbPrmJobSite.DataSource = InfoList;
            cmbPrmJobSite.DataBind();
        }
        public void ListSubScore()
        {
            ScoreSubBLL subBLL = new ScoreSubBLL();

            DataTable dt = new DataTable();
            dt = subBLL.select(Int32.Parse(Session["PRMID"].ToString())
                , Int32.Parse(cmbAprJobSite.SelectedValue), (double?)null);
            cmbSubScore.DataSource = dt;
            cmbSubScore.DataBind();
        }
        protected void btnFindEmp_Click(object sender, ImageClickEventArgs e)
        {
            try
            {
                string st = Hide.Value;

                IList<EmployeeInfo> infoList = empBLL.ListEmployees(st);
                if (infoList.Count > 0)
                    infoEmp = infoList[0];

                lblID_emp.Text = infoEmp.Sequence.ToString();
                txtEmp.Text = infoEmp.Person.FullName;
                lblPositionName.Text = infoEmp.Position.RefNo + " - " + infoEmp.Position.Name;
                lblClassName.Text = infoEmp.Position.ClassSpecification.Name;

                if (infoEmp.WorkingJobSite != null)
                {
                    cmbAprJobSite.SelectedValue = infoEmp.WorkingJobSite.Sequence.ToString();
                    cmbPrmJobSite.SelectedValue = infoEmp.WorkingJobSite.Sequence.ToString();
                }
            }
            catch
            {

            }
        }
        protected void btnFindEmp_Load(object sender, EventArgs e)
        {
            if ((DataItem is Telerik.Web.UI.GridInsertionObject))
            {
                string ETID = Session["ETID"].ToString();
                string JSID = Session["JSID"].ToString();
                btnFindEmp.Attributes.Add("OnClick", "LinkMDdetail(" + Hide.ClientID + "," + "'Forms/DialogEmp.aspx?ETID=" + ETID + "&JSID=" + JSID + "','../frmIframe.html',700,500,modal:true)");
            }
        }

        protected void btnAddnew_Click(object sender, EventArgs e)
        {
            prBLL.InsertPromotionEmp(Int32.Parse(Session["PRMID"].ToString())
                , Int32.Parse(lblID_emp.Text)
                , Int32.Parse(cmbPrmJobSite.SelectedValue)
                , Int32.Parse(cmbAprJobSite.SelectedValue)
                , Int32.Parse(rdoPromote.SelectedValue)
                , double.Parse(txtScore.Text)
                , 0//double.Parse(txtSalary.Text)
                , 0
                , 0//double.Parse(txtPercen.Text)
                , 0//double.Parse(txtAmount.Text)
                , 0//Int32.Parse(cmbSubScore.SelectedValue)
                , txtRemarks.Text
                , ssMng.EmpSeqI);
        }

        protected void btnUpdate_Click(object sender, EventArgs e)
        {
            prBLL.UpdatePromotionEmp(Convert.ToInt32(lblSequence.Text.Trim())
                , Int32.Parse(Session["PRMID"].ToString())
                , Int32.Parse(lblEmpSeq.Text)
                , Int32.Parse(cmbPrmJobSite.SelectedValue)
                , Int32.Parse(cmbAprJobSite.SelectedValue)
                , Int32.Parse(rdoPromote.SelectedValue)
                , double.Parse(txtScore.Text)
                , 0//double.Parse(txtSalary.Text)
                , 0
                , 0//double.Parse(txtPercen.Text)
                , 0//double.Parse(txtAmount.Text)
                , 0//Int32.Parse(cmbSubScore.SelectedValue)
                , txtRemarks.Text
                , ssMng.EmpSeqI);
        }

        protected void btnCancel_Click(object sender, EventArgs e)
        {

        }

        protected void txtScore_TextChanged(object sender, EventArgs e)
        {
            cmbSubScore.SelectedValue = prBLL.GetSubScoreLevel(Int32.Parse(Session["PRMID"].ToString())
                , Int32.Parse(cmbAprJobSite.SelectedValue)
                , double.Parse(txtScore.Text)).ToString();
        }

        protected void cmbAprJobSite_SelectedIndexChanged(object o, RadComboBoxSelectedIndexChangedEventArgs e)
        {
            ListSubScore();
        }
    }
}